<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label">父菜单</label>
        <div class="layui-input-block">
            <div class="layui-inline" style="width: 235px;">
                <input type="text"  placeholder="不填则为最顶级菜单" autocomplete="off" class="layui-input" id="demo">
                <input type="hidden" name="parentId" value="0" >

            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">菜单名称</label>
        <div class="layui-input-block">
            <input type="text" name="menuName" lay-verify="required" lay-reqtext="菜单名称不能为空" placeholder="请输入用菜单名称" value="" class="layui-input">
            <tip>填写菜单名称。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单图标</label>
        <div class="layui-input-block">
            <input type="text" name="menuIcon" placeholder="请输入用菜单名称" value="" class="layui-input">
            <tip>填写菜单图标。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单路径</label>
        <div class="layui-input-block">
            <input type="text" name="path"  placeholder="请输入用菜单路径" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">打开方式</label>
        <div class="layui-input-block">
            <input type="radio" name="openType" value="0" title="_self" checked="">
            <input type="radio" name="openType" value="1" title="_blank">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">菜单类型</label>
        <div class="layui-input-block">
            <input type="radio" name="menuType" value="0" title="目录" checked="">
            <input type="radio" name="menuType" value="1" title="菜单">
            <input type="radio" name="menuType" value="2" title="按钮">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">显示顺序</label>
        <div class="layui-input-block">
            <input type="number" name="orderNum" lay-verify="required" lay-reqtext="手机不能为空" placeholder="请输入手机" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限列表</label>
        <div class="layui-input-block">
            <input type="text" name="perms" placeholder="权限列表"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">显示或隐藏</label>
        <div class="layui-input-block">
            <input type="radio" name="visible" value="0" title="显示" checked="">
            <input type="radio" name="visible" value="1" title="隐藏">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="正常" checked="">
            <input type="radio" name="status" value="1" title="停用">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js?v=2.0.0}" charset="utf-8"></script>

<script th:inline="none">
    layui.use(['form','layer','tableSelect'], function () {
        var form = layui.form,
            layer = layui.layer;
        var tableSelect = layui.tableSelect;
        var $ = layui.jquery;


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            console.log(JSON.stringify(data.field));
            if(1==1){
                $.ajax(
                {
                    url:"/sys-menu/add",
                    type: "post", 
                    async: false,
                    data: JSON.stringify(data.field),
                    contentType: 'application/json',
                    success:function(result){
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                                console.log(  parent.layui  );
                                console.log(  parent.layui.$('#btn-refresh')  );
                                parent.layui.$('#btn-refresh').click();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                }
            );
            }
            

            // var index = layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // }, function () {

            //     // 关闭弹出层
            //     layer.close(index);

            //     var iframeIndex = parent.layer.getFrameIndex(window.name);
            //     parent.layer.close(iframeIndex);

            // });

            return false;
        });

        tableSelect.render({
                elem: '#demo',	//定义输入框input对象 必填
                checkedKey: 'menuId', //表格的唯一建值，非常重要，影响到选中状态 必填
                searchKey: 'menuName',	//搜索输入框的name值 默认keyword
                searchPlaceholder: '菜单名称',	//搜索输入框的提示文字 默认关键词搜索
                height:'400',  //自定义高度
                width:'900',  //自定义宽度
                table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                    url: '/sys-menu/query',
                    method: "post",
                    cols: [[
                        { type: 'radio' },
                        {field: 'menuName', minWidth: 200, title: '菜单名称'},
                        {field: 'perms', title: '权限标识'},
                        {field: 'path', title: '菜单url'},
                        {field: 'orderNum', width: 80, align: 'center', title: '排序号'},
                        {
                            field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                                if (d.menuType == 0) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                                }
                                if (d.menuType == 1) {
                                    return '<span class="layui-badge-rim">菜单</span>';
                                } else {
                                    return '<span class="layui-badge layui-bg-gray">按钮</span>';
                                }
                            }, title: '类型'
                        }
                    ]]
                },
                done: function (elem, data) {
                    //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                    //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                    console.log(data);
                    var NEWJSON = [];
                    var names=[];
                    layui.each(data.data, function (index, item) {
                        NEWJSON.push(item.menuName);
                        names.push(item.menuId);
                    })
                    elem.val(NEWJSON.join(","));
                    $("[name='parentId']").val(names.join(','));
                }
            })

        // var data1=[];
        // $.ajax(
        //     {
        //         url:"/sys-menu/tree/query",
        //         type: "post", 
        //         async: false,
        //         success:function(result){
        //             // console.log(result);
        //             data1=result.data;
        //         }
        //     }
        // );

        // // 绑定输入框
        // dropdown.render({
        //     elem: '#ID-dropdown-demo-base-input',
        //     data: data1,
        //     clickScope: "all",
        //     click: function(obj){
        //         console.log(obj);
        //         this.elem.val(obj.title);
        //         return false;
        //     },
        //     style: 'min-width: 235px;'
        // });

    });
</script>
</body>
</html>